package com.easy.dfc.web;

import com.alibaba.fastjson.JSONObject;
import com.easy.core.web.BaseController;
import com.easy.dfc.Utils.JsonUtil;
import com.easy.dfc.Utils.PagingUtil;
import com.easy.dfc.Utils.Util;
import com.easy.dfc.Utils.Error;
import com.easy.dfc.entity.*;
import com.easy.dfc.service.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by VULCANTSeries on 2017/5/9.
 */
@Controller
@RequestMapping("${apiPath}/promotion")
public class PromotionController extends BaseController {

    public static long DayFor7 = 7 * 24 * 60 * 60;

    public static long DayFor3 = 3 * 24 * 60 * 60;

    @Resource
    private TiaoweihuosetService tiaoweihuosetService;

    @Resource
    private TiantiantejiaService tiantiantejiaService;

    @Resource
    private TuangouService tuangouService;

    @Resource
    private ProductsetService productsetService;

    @Resource
    private UsersetService usersetService;

    /**
     * 根据传入的时间和页码下标返回符合要求的挑尾货活动列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTiaoweihuoList", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getTiaoweihuoList(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        List<TiaoweihuosetWithBLOBs> tiaoweihuosetWithBLOBsList;
        try {
            long time = Long.parseLong(request.getParameter("Time"));
            int start = Integer.parseInt(request.getParameter("Page"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setTime(time);
            pagingUtil.setStart((start - 1) * 20);
            tiaoweihuosetWithBLOBsList = tiaoweihuosetService.getTiaoweihuoList(pagingUtil);
            String str = JSONObject.toJSONString(tiaoweihuosetWithBLOBsList);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 根据传入的时间和页码下标返回该场次的挑尾货活动列表，时间必须一致
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTiaoweihuoInTime", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getTiaoweihuoInTime(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        List<TiaoweihuosetWithBLOBs> tiaoweihuosetWithBLOBsList;
        try {
            long time = Long.parseLong(request.getParameter("Time"));
            int start = Integer.parseInt(request.getParameter("Page"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setTime(time);
            pagingUtil.setStart((start - 1) * 20);
            tiaoweihuosetWithBLOBsList = tiaoweihuosetService.getTiaoweihuoInTime(pagingUtil);
            String str = JSONObject.toJSONString(tiaoweihuosetWithBLOBsList);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 挑尾货活动报名
     * 先根据传入的ProductId判断是否有这个宝贝，然后修改宝贝活动信息并在活动表里加上这个宝贝的活动
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/applyTiaoweihuo", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> applyTiaoweihuo(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            String ProductId = Util.ToUuidString(request.getParameter("ProductId").replaceAll("-", ""));
            ProductsetWithBLOBs productsetWithBLOBs = productsetService.selectByProductId(ProductId);
            if (productsetWithBLOBs == null) {
                jsonObject.put("Err", Error.NotProduct);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            int num = Integer.parseInt(request.getParameter("Num"));
            //判断库存是否足够
            Stock stock = tiaoweihuosetService.stockCheck(productsetWithBLOBs.getId() + "");
            if (stock.getStock() < num) {
                jsonObject.put("Err", Error.OverStock);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            //如果该活动已报名并且没有过期，则不能报名
            long start = Long.parseLong(request.getParameter("Start"));
            TiaoweihuosetWithBLOBs tiaoweihuosetWithBLOBs;
            if (ValidatedApply(ProductId, start)) {
                jsonObject.put("Err", Error.InPromotion);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            } else {
                tiaoweihuosetWithBLOBs = new TiaoweihuosetWithBLOBs();
            }
            BigDecimal price = BigDecimal.valueOf(Double.parseDouble(request.getParameter("Price")));
            productsetWithBLOBs.setPromotionprice(price);
            productsetWithBLOBs.setPromotiontype("挑尾货");
            long end = start + DayFor7;
            long telephone = Long.parseLong(request.getParameter("Telephone"));
            String title = request.getParameter("Title");
            tiaoweihuosetWithBLOBs.setNum(num);
            tiaoweihuosetWithBLOBs.setPrice(productsetWithBLOBs.getPrice());
            tiaoweihuosetWithBLOBs.setPromotionprice(price);
            tiaoweihuosetWithBLOBs.setStart(start);
            tiaoweihuosetWithBLOBs.setEnd(end);
            tiaoweihuosetWithBLOBs.setDetail(productsetWithBLOBs.getDetail());
            tiaoweihuosetWithBLOBs.setItemno(productsetWithBLOBs.getItemno());
            tiaoweihuosetWithBLOBs.setMainimg(productsetWithBLOBs.getMainimg());
            tiaoweihuosetWithBLOBs.setName(productsetWithBLOBs.getName());
            tiaoweihuosetWithBLOBs.setUnit(productsetWithBLOBs.getUnit());
            tiaoweihuosetWithBLOBs.setBrandId(productsetWithBLOBs.getBrandId());
            tiaoweihuosetWithBLOBs.setIsenabled(productsetWithBLOBs.getIsenabled());
            tiaoweihuosetWithBLOBs.setProductid(productsetWithBLOBs.getProductid());
            tiaoweihuosetWithBLOBs.setTagId(productsetWithBLOBs.getTagId());
            tiaoweihuosetWithBLOBs.setTelephone(telephone);
            tiaoweihuosetWithBLOBs.setTitle(title);
            tiaoweihuosetWithBLOBs.setCreated(productsetWithBLOBs.getCreated());
            tiaoweihuosetWithBLOBs.setStuts(0);
            productsetService.updateByPrimaryKeySelective(productsetWithBLOBs);
            tiaoweihuosetService.insertSelective(tiaoweihuosetWithBLOBs);
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 根据传入的时间和页码下标返回符合要求的聚爆款活动列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTiantiantejiaList", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getTiantiantejiaList(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        List<TiantiantejiaWithBLOBs> tiantiantejiaWithBLOBsList;
        try {
            long time = Long.parseLong(request.getParameter("Time"));
            int start = Integer.parseInt(request.getParameter("Page"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setTime(time);
            pagingUtil.setStart((start - 1) * 20);
            tiantiantejiaWithBLOBsList = tiantiantejiaService.getTiantiantejiaList(pagingUtil);
            String str = JSONObject.toJSONString(tiantiantejiaWithBLOBsList);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 根据传入的时间和页码下标返回该场次的聚爆款活动列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTiantiantejiaInTime", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getTiantiantejiaInTime(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        List<TiantiantejiaWithBLOBs> tiantiantejiaWithBLOBsList;
        try {
            long time = Long.parseLong(request.getParameter("Time"));
            int start = Integer.parseInt(request.getParameter("Page"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setTime(time);
            pagingUtil.setStart((start - 1) * 20);
            tiantiantejiaWithBLOBsList = tiantiantejiaService.getTiantiantejiaInTime(pagingUtil);
            String str = JSONObject.toJSONString(tiantiantejiaWithBLOBsList);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 聚爆款活动报名
     * 先根据传入的ProductId判断是否有这个宝贝，然后修改宝贝活动信息并在活动表里加上这个宝贝的活动
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/applyTiantiantejia", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> applyTiantiantejia(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            String ProductId = Util.ToUuidString(request.getParameter("ProductId").replaceAll("-", ""));
            ProductsetWithBLOBs productsetWithBLOBs = productsetService.selectByProductId(ProductId);
            if (productsetWithBLOBs == null) {
                jsonObject.put("Err", Error.NotProduct);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            int num = Integer.parseInt(request.getParameter("Num"));
            //判断库存是否足够
            Stock stock = tiantiantejiaService.stockCheck(productsetWithBLOBs.getId() + "");
            if (stock.getStock() < num) {
                jsonObject.put("Err", Error.OverStock);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            //判断销量是否足够
            stock = tiantiantejiaService.ValidSales(productsetWithBLOBs.getProductid() + "");
            if (stock.getStock() == 0) {
                jsonObject.put("Err", Error.SalesNotEnough);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            //如果该活动已报名并且没有过期，则不能报名
            long start = Long.parseLong(request.getParameter("Start"));
            TiantiantejiaWithBLOBs tiantiantejiaWithBLOBs;
            if (ValidatedApply(ProductId, start)) {
                jsonObject.put("Err", Error.InPromotion);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            } else {
                tiantiantejiaWithBLOBs = new TiantiantejiaWithBLOBs();
            }
            BigDecimal price = BigDecimal.valueOf(Double.parseDouble(request.getParameter("Price")));
            productsetWithBLOBs.setPromotionprice(price);
            productsetWithBLOBs.setPromotiontype("聚爆款");
            long end = start + DayFor3;
            long telephone = Long.parseLong(request.getParameter("Telephone"));
            String title = request.getParameter("Title");
            tiantiantejiaWithBLOBs.setNum(num);
            tiantiantejiaWithBLOBs.setPrice(productsetWithBLOBs.getPrice());
            tiantiantejiaWithBLOBs.setPromotionprice(price);
            tiantiantejiaWithBLOBs.setStart(start);
            tiantiantejiaWithBLOBs.setEnd(end);
            tiantiantejiaWithBLOBs.setDetail(productsetWithBLOBs.getDetail());
            tiantiantejiaWithBLOBs.setItemno(productsetWithBLOBs.getItemno());
            tiantiantejiaWithBLOBs.setMainimg(productsetWithBLOBs.getMainimg());
            tiantiantejiaWithBLOBs.setName(productsetWithBLOBs.getName());
            tiantiantejiaWithBLOBs.setUnit(productsetWithBLOBs.getUnit());
            tiantiantejiaWithBLOBs.setBrandId(productsetWithBLOBs.getBrandId());
            tiantiantejiaWithBLOBs.setIsenabled(productsetWithBLOBs.getIsenabled());
            tiantiantejiaWithBLOBs.setProductid(productsetWithBLOBs.getProductid());
            tiantiantejiaWithBLOBs.setTagId(productsetWithBLOBs.getTagId());
            tiantiantejiaWithBLOBs.setTelephone(telephone);
            tiantiantejiaWithBLOBs.setTitle(title);
            tiantiantejiaWithBLOBs.setCreated(productsetWithBLOBs.getCreated());
            tiantiantejiaWithBLOBs.setStuts(0);
            productsetService.updateByPrimaryKeySelective(productsetWithBLOBs);
            tiantiantejiaService.insertSelective(tiantiantejiaWithBLOBs);
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 根据传入的时间和页码下标返回符合要求的团购活动列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTuangouList", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getTuangouList(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        List<TuangouWithBLOBs> tuangouWithBLOBsList;
        try {
            long time = Long.parseLong(request.getParameter("Time"));
            int start = Integer.parseInt(request.getParameter("Page"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setTime(time);
            pagingUtil.setStart((start - 1) * 20);
            tuangouWithBLOBsList = tuangouService.getTuangouList(pagingUtil);
            String str = JSONObject.toJSONString(tuangouWithBLOBsList);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 根据传入的时间和页码下标返回该场次的团购活动列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTuangouInTime", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getTuangouInTime(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        List<TuangouWithBLOBs> tuangouWithBLOBsList;
        try {
            long time = Long.parseLong(request.getParameter("Time"));
            int start = Integer.parseInt(request.getParameter("Page"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setTime(time);
            pagingUtil.setStart((start - 1) * 20);
            tuangouWithBLOBsList = tuangouService.getTuangouInTime(pagingUtil);
            String str = JSONObject.toJSONString(tuangouWithBLOBsList);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 团购活动报名
     * 先根据传入的ProductId判断是否有这个宝贝，然后修改宝贝活动信息并在活动表里加上这个宝贝的活动
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/applyTuangou", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> applyTuangou(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            String ProductId = Util.ToUuidString(request.getParameter("ProductId").replaceAll("-", ""));
            ProductsetWithBLOBs productsetWithBLOBs = productsetService.selectByProductId(ProductId);
            if (productsetWithBLOBs == null) {
                jsonObject.put("Err", Error.NotProduct);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            int maxnum = Integer.parseInt(request.getParameter("MaxNum"));
            int minnum = Integer.parseInt(request.getParameter("MinNum"));
            //判断库存是否足够
            Stock stock = tiantiantejiaService.stockCheck(productsetWithBLOBs.getId() + "");
            if (stock.getStock() < maxnum) {
                jsonObject.put("Err", Error.OverStock);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            //如果该活动已报名并且没有过期，则不能报名
            long start = Long.parseLong(request.getParameter("Start"));
            TuangouWithBLOBs tuangouWithBLOBs;
            if (ValidatedApply(ProductId, start)) {
                jsonObject.put("Err", Error.InPromotion);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            } else {
                tuangouWithBLOBs = new TuangouWithBLOBs();
            }
            BigDecimal price = BigDecimal.valueOf(Double.parseDouble(request.getParameter("Price")));
            productsetWithBLOBs.setPromotionprice(price);
            productsetWithBLOBs.setPromotiontype("团购");
            long end = start + DayFor3;
            long telephone = Long.parseLong(request.getParameter("Telephone"));
            String title = request.getParameter("Title");
            tuangouWithBLOBs.setMaxnum(maxnum);
            tuangouWithBLOBs.setMinnum(minnum);
            tuangouWithBLOBs.setPrice(productsetWithBLOBs.getPrice());
            tuangouWithBLOBs.setPromotionprice(price);
            tuangouWithBLOBs.setStart(start);
            tuangouWithBLOBs.setEnd(end);
            tuangouWithBLOBs.setDetail(productsetWithBLOBs.getDetail());
            tuangouWithBLOBs.setItemno(productsetWithBLOBs.getItemno());
            tuangouWithBLOBs.setMainimg(productsetWithBLOBs.getMainimg());
            tuangouWithBLOBs.setName(productsetWithBLOBs.getName());
            tuangouWithBLOBs.setUnit(productsetWithBLOBs.getUnit());
            tuangouWithBLOBs.setBrandId(productsetWithBLOBs.getBrandId());
            tuangouWithBLOBs.setIsenabled(productsetWithBLOBs.getIsenabled());
            tuangouWithBLOBs.setProductid(productsetWithBLOBs.getProductid());
            tuangouWithBLOBs.setTagId(productsetWithBLOBs.getTagId());
            tuangouWithBLOBs.setTelephone(telephone);
            tuangouWithBLOBs.setTitle(title);
            tuangouWithBLOBs.setCreated(productsetWithBLOBs.getCreated());
            tuangouWithBLOBs.setStuts(0);
            productsetService.updateByPrimaryKeySelective(productsetWithBLOBs);
            tuangouService.insertSelective(tuangouWithBLOBs);
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 卖家查看活动列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getPromotionList", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getPromotionList(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        List<TiaoweihuosetWithBLOBs> tiaoweihuosetWithBLOBsList = new ArrayList<>();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            //type:查询类型，1:全店活动，2:挑尾货当前活动，3:挑尾货往期活动，4:聚爆款当前活动，5:聚爆款往期活动，6:团购当前活动，7:团购往期活动
            String type = request.getParameter("Type");
            long time = Long.parseLong(request.getParameter("Time"));
            int start = Integer.parseInt(request.getParameter("Page"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setTime(time);
            pagingUtil.setStart((start - 1) * 20);
            String str = "";
            if ("1".equals(type)) {
                int FactoryId = Integer.parseInt(request.getParameter("FactoryId"));
                pagingUtil.setBrandId(FactoryId);
                tiaoweihuosetWithBLOBsList = tiaoweihuosetService.selectByFactoryNow(pagingUtil);
                List<TiantiantejiaWithBLOBs> tiantiantejiaWithBLOBsList = tiantiantejiaService.selectByFactoryNow(pagingUtil);
                List<TuangouWithBLOBs> tuangouWithBLOBsList = tuangouService.selectByFactoryNow(pagingUtil);
                str = "[{\"tiaoweihuo\":" + JSONObject.toJSONString(tiaoweihuosetWithBLOBsList) + "},{\"jubaokuan\":" + JSONObject.toJSONString(tiantiantejiaWithBLOBsList) + "},{\"tuangou\":" + JSONObject.toJSONString(tuangouWithBLOBsList) + "}]";
            } else if ("2".equals(type)) {
                int brandId = Integer.parseInt(request.getParameter("BrandId"));
                pagingUtil.setBrandId(brandId);
                tiaoweihuosetWithBLOBsList = tiaoweihuosetService.selectByBrandNow(pagingUtil);
                str = JSONObject.toJSONString(tiaoweihuosetWithBLOBsList);
            } else if ("3".equals(type)) {
                int brandId = Integer.parseInt(request.getParameter("BrandId"));
                pagingUtil.setBrandId(brandId);
                tiaoweihuosetWithBLOBsList = tiaoweihuosetService.selectByBrandBefore(pagingUtil);
                str = JSONObject.toJSONString(tiaoweihuosetWithBLOBsList);
            } else if ("4".equals(type)) {
                int brandId = Integer.parseInt(request.getParameter("BrandId"));
                pagingUtil.setBrandId(brandId);
                List<TiantiantejiaWithBLOBs> tiantiantejiaWithBLOBsList = tiantiantejiaService.selectByBrandNow(pagingUtil);
                str = JSONObject.toJSONString(tiantiantejiaWithBLOBsList);
            } else if ("5".equals(type)) {
                int brandId = Integer.parseInt(request.getParameter("BrandId"));
                pagingUtil.setBrandId(brandId);
                List<TiantiantejiaWithBLOBs> tiantiantejiaWithBLOBsList = tiantiantejiaService.selectByBrandBefore(pagingUtil);
                str = JSONObject.toJSONString(tiantiantejiaWithBLOBsList);
            } else if ("6".equals(type)) {
                int brandId = Integer.parseInt(request.getParameter("BrandId"));
                pagingUtil.setBrandId(brandId);
                List<TuangouWithBLOBs> tuangouWithBLOBsList = tuangouService.selectByBrandNow(pagingUtil);
                str = JSONObject.toJSONString(tuangouWithBLOBsList);
            } else if ("7".equals(type)) {
                int brandId = Integer.parseInt(request.getParameter("BrandId"));
                pagingUtil.setBrandId(brandId);
                List<TuangouWithBLOBs> tuangouWithBLOBsList = tuangouService.selectByBrandBefore(pagingUtil);
                str = JSONObject.toJSONString(tuangouWithBLOBsList);
            }
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 店铺首页活动列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getFactoryHomePromotion", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getFactoryHomePromotion(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            long time = Long.parseLong(request.getParameter("Time"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setTime(time);
            String str = "";
            int FactoryId = Integer.parseInt(request.getParameter("FactoryId"));
            pagingUtil.setBrandId(FactoryId);
            List<TiaoweihuosetWithBLOBs> tiaoweihuosetWithBLOBsList = tiaoweihuosetService.selectByFactoryNowFor3(pagingUtil);
            List<TiantiantejiaWithBLOBs> tiantiantejiaWithBLOBsList = tiantiantejiaService.selectByFactoryNowFor3(pagingUtil);
            List<TuangouWithBLOBs> tuangouWithBLOBsList = tuangouService.selectByFactoryNowFor3(pagingUtil);
            str = "[{\"tiaoweihuo\":" + JSONObject.toJSONString(tiaoweihuosetWithBLOBsList) + "},{\"jubaokuan\":" + JSONObject.toJSONString(tiantiantejiaWithBLOBsList) + "},{\"tuangou\":" + JSONObject.toJSONString(tuangouWithBLOBsList) + "}]";
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    //验证是否已经报过活动并且活动时间冲突
    private boolean ValidatedApply(String ProductId, long start) {
        List<TiantiantejiaWithBLOBs> tiantiantejiaWithBLOBs = tiantiantejiaService.getTiantiantejia(ProductId);
        List<TiaoweihuosetWithBLOBs> tiaoweihuosetWithBLOBs = tiaoweihuosetService.getTiaoweihuo(ProductId);
        List<TuangouWithBLOBs> tuangouWithBLOBs = tuangouService.getTuangou(ProductId);
        if (tiantiantejiaWithBLOBs != null) {
            for (TiantiantejiaWithBLOBs t : tiantiantejiaWithBLOBs) {
                if (t.getEnd() > start) {
                    return true;
                }
            }
        }
        if (tiaoweihuosetWithBLOBs != null) {
            for (TiaoweihuosetWithBLOBs t : tiaoweihuosetWithBLOBs) {
                if (t.getEnd() > start) {
                    return true;
                }
            }
        }
        if (tuangouWithBLOBs != null) {
            for (TuangouWithBLOBs t : tuangouWithBLOBs) {
                if (t.getEnd() > start) {
                    return true;
                }
            }
        }
        return false;
    }
}
